package com.xxh.market.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//指定过滤器名字，要拦截路径
@WebFilter(filterName = "EncodeFilter" ,urlPatterns = "/*")
public class EncodeFilter implements Filter {

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)resp;
        //设置请求编码
        request.setCharacterEncoding("UTF-8");
        //设置响应编码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html,charset=utf-8");
        //放行 请求与响应
        chain.doFilter(req, resp);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
